package top;

import dao.StoresManagement;
import dao.StoresManager;
import dao.UsersManagement;
import dao.UsersManager;
import service.AccountService;
import service.AccountServiceImp;
import service.StoreService;
import service.StoreServiceImp;

import util.Common;

import java.util.Properties;

public class ConfigCenter {

    // DAO //////////////////////////////////////////////////////////////////////////////////////////////////

    private static final UsersManagement  userManager    = new UsersManager();
    private static final StoresManagement storesManager  = new StoresManager();

    // SERVICE //////////////////////////////////////////////////////////////////////////////////////////////

    private static final AccountService   accountService = new AccountServiceImp(userManager);
    private static final StoreService     storeService   = new StoreServiceImp  (storesManager, accountService);
    //-------------------------------------------------------------------------------------------------------
    public static final AccountService getAccountService() { return accountService; }
    public static final StoreService   getStoreService()   { return storeService; }

    // LOCAL-FILESYSTEM /////////////////////////////////////////////////////////////////////////////////////

    public static final String CONFIGHOME                = Common.initDirectory("./etc");
    public static final String DATAHOME                  = "/data"; // at web app root dir/data

    public static final String STOREPICHOME              = DATAHOME + "/store_pictures";

    public static final String DBCONFIGFILE              = CONFIGHOME + "/database.cfg";
    public static final String FILEUPLOADCONFIGFILE      = CONFIGHOME + "/fileUpload.cfg";
    public static final String ACCOUNTCONFIGFILE         = CONFIGHOME + "/account.cfg";

    // CONFIG-PROPERTIES ////////////////////////////////////////////////////////////////////////////////////

    public static Properties getFileUploadConfig()        { return fileUploadConfig;}
    public static String getFileUploadConfig(String prop) { return fileUploadConfig.getProperty(prop); }
    public static Properties getDatabaseConfig()          { return databaseConfig; }
    public static String getDatabaseConfig(String prop)   { return databaseConfig.getProperty(prop); }
    public static Properties getAccountConfig()           { return accountConfig; }
    public static String getAccountConfig(String prop)    { return accountConfig.getProperty(prop); }

    // INIT-FILEUPLOAING-CONFIG /////////////////////////////////////////////////////////////////////////////

    private static Properties fileUploadConfig = initFileUploadConfig();

    private static Properties initFileUploadConfig() {
        Properties defaultCfg = new Properties();
        defaultCfg.put("DEFAULT_MEMORY_THRESHOLD", "4194304");  // 4MB
        defaultCfg.put("DEFAULT_MAX_FILE_SIZE",    "33554432"); //32MB
        defaultCfg.put("DEFAULT_MAX_REQUEST_SIZE", "67108864"); //64MB
        return Common.initConfig(defaultCfg, FILEUPLOADCONFIGFILE);
    }

    // INIT-DATASOURCE //////////////////////////////////////////////////////////////////////////////////////

    private static Properties databaseConfig = initDatabaseConfig();

    private static Properties initDatabaseConfig() {
        Properties defaultCfg = new Properties();
        defaultCfg.put("dbname", "iweb271");
        defaultCfg.put("port", "3306");
        defaultCfg.put("ip", "47.94.91.248");
        defaultCfg.put("dbuser", "iweb271");
        defaultCfg.put("dbpasswd", "iwebgogogo");
        defaultCfg.put("connectionUsingTimeOut", "16");
        defaultCfg.put("connectionIdleTimeOut", "32");
        defaultCfg.put("minConnectionCount", "4");
        defaultCfg.put("maxConnectionCount", "64");

        return Common.initConfig(defaultCfg, DBCONFIGFILE);
    }

    // INIT-ACCOUNT-CONFIG //////////////////////////////////////////////////////////////////////////////////

    private static Properties accountConfig = initAccountConfig();

    private static Properties initAccountConfig() {
        Properties defaultCfg = new Properties();
        defaultCfg.put("autoLogingLifeTime", "604800"); //7Days
        return Common.initConfig(defaultCfg, ACCOUNTCONFIGFILE);
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////
}
